//
//  ZPViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/8/2.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "ZPViewController.h"
#import "FindCollectionViewCell.h"
#import "XiangQingViewController.h"
#import "FindLayout.h"
#import "ArtGalleryCell_third.h"
#import "IGZPApi.h"
#import "UIColor+iGallery.h"
#import "UIView+Frame.h"

@interface ZPViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
{
    UICollectionView *collect;
    UILabel *msg;
}
@property (nonatomic, copy) NSString *pageNum;
@property (nonatomic, copy) NSString *pageSize;
@property (nonatomic, strong)IGZPApi *api;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation ZPViewController


- (void)viewDidLoad
{
    [super viewDidLoad];    
    FindLayout *layout = [[FindLayout alloc] init];    
    collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT-49-20-INTROHEIGHT) collectionViewLayout:layout];
    [collect registerClass:[ArtGalleryCell_third class] forCellWithReuseIdentifier:@"ArtGalleryCell_third"];
    collect.alwaysBounceVertical = YES;
    collect.scrollIndicatorInsets = UIEdgeInsetsMake(BANNERHEIGHT+44, 0, 0, 0);
    collect.delegate = self;
    collect.dataSource = self;
    collect.backgroundColor = RGB(234, 234, 234);
    [self.view addSubview:collect];
    
    
    [collect ig_setHeaderWithRefreshingBlock:^{
        _pageSize = @"10";
        _pageNum = @"1";
        [self dataRequest];
    }];
    self.api = [IGZPApi new];
    
    _pageSize = @"10";
    _pageNum = @"1";
    self.dataSource = [@[] mutableCopy];
    [self dataRequest];
}


- (void)dataRequest
{
    self.api.pageSize = _pageSize;
    self.api.pageNum = _pageNum;
    self.api.u_id = _u_id;
    @KWeakObj(self)
    [self.api startWithDecodeClass:[IGZPResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [collect ig_endHeaderRefreshing];
        [collect ig_endFooterRefreshing];
        [selfWeak hidden];
        if (responseObject)
        {
            IGZPResp *resp = responseObject;
            IGZPModel *model = resp.data;
            if (model.ProductList && model.ProductList.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                if ([_pageNum isEqualToString:@"1"] && _dataSource.count < 1)
                {
                    [selfWeak show];
                    return;
                }
                [collect ig_endFooterRefreshingWithNoMoreData];
            }
            if ([_pageNum isEqualToString:@"1"])
            {
                _dataSource = [NSMutableArray arrayWithArray:model.ProductList];
            }
            else
            {
                [_dataSource addObjectsFromArray:model.ProductList];
            }
            [collect reloadData];
        }
        else
        {
            if (_dataSource.count < 1)
            {
                [selfWeak show];
            }
        }
    }];
}


- (void)setupFooter
{
    @KWeakObj(self)
    [collect ig_setFooterWithRefreshingBlock:^{
        _pageNum = [NSString stringWithFormat:@"%ld", [_pageNum integerValue] + 1];
        [selfWeak dataRequest];
    }];
}



- (void)show
{
    if (!msg)
    {
        msg = [[UILabel alloc] initWithFrame:CGRectZero];
//        msg.text = @"TA还没有作品...";
        msg.text = NSLocalizedString(@"No Artworks Available", nil);
        msg.textAlignment = NSTextAlignmentCenter;
        msg.textColor = [UIColor c4Color];
    }
    [self.view addSubview:msg];
    msg.width = self.view.width - 40;
    msg.height = self.view.height;
    msg.center = CGPointMake(self.view.width/2, self.view.height/2);
}

- (void)hidden
{
    [msg removeFromSuperview];
}

#pragma mark - UICollectionViewDelegateFlowLayout, UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count?:0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ArtGalleryCell_third *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtGalleryCell_third" forIndexPath:indexPath];
    IGArtWorkModel *base = _dataSource[indexPath.item];
    cell.imgUrl = base.image;
    cell.titleL = base.title;
    [cell setNeedsLayout];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel *base = _dataSource[indexPath.item];
    XiangQingViewController *vc = [[XiangQingViewController alloc] init];
    vc.p_id = base.aId;
    vc.isRoot = YES;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGArtWorkModel *base = _dataSource[indexPath.item];
    if ([base.plates integerValue]==1)
    {
        //横板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1080/1920));
    }
    else if ([base.plates integerValue]==2)
    {
        //坚板
        return CGSizeMake((KSCREENWIDTH-16)/2, ((KSCREENWIDTH-16)/2*1920/1080));
    }
    return CGSizeMake(0, 0);
}

@end
